//
//  LLCountDownButton.m
//  Receipt
//
//  Created by admin on 2019/3/5.
//  Copyright © 2019 admin. All rights reserved.
//

#import "LLCountDownButton.h"

@interface LLCountDownButton ()
{
    NSUInteger _countDown;
}

@property (nonatomic, strong) NSTimer *timer;

@end

@implementation LLCountDownButton

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.enabled = NO;
        _countDown = 60;
        self.count = 60;
        self.layer.borderColor = [UIColor redColor].CGColor;
        self.layer.borderWidth = 1;
        self.layer.cornerRadius = 5;
        [self setTitle:@"获取验证码" forState:UIControlStateNormal];
        [self setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [self addTarget:self action:@selector(countDownButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        self.titleLabel.font = [UIFont systemFontOfSize:14];
        
    }
    return self;
}

- (void)setNormalBorderColor:(UIColor *)normalBorderColor
{
    _normalBorderColor = normalBorderColor;
    self.layer.borderColor = normalBorderColor.CGColor;
}

- (void)setNormalTitleColor:(UIColor *)normalTitleColor
{
    _normalTitleColor = normalTitleColor;
    [self setTitleColor:normalTitleColor forState:UIControlStateNormal];
}

- (void)setDisableBorderColor:(UIColor *)disableBorderColor
{
    _disableBorderColor = disableBorderColor;
}

- (void)setDisableTitleColor:(UIColor *)disableTitleColor
{
    _disableTitleColor = disableTitleColor;
}

- (void)setCount:(NSUInteger)count
{
    _count = count;
    _countDown = count;
}

- (void)setEnabled:(BOOL)enabled
{
    if (_countDown < 60 && _countDown >= 0) {
        enabled = NO;
    }
    if (enabled) {
        [self setTitleColor:self.normalTitleColor forState:UIControlStateNormal];
        self.layer.borderColor = self.normalBorderColor.CGColor;
    } else {
        self.layer.borderColor = self.disableBorderColor.CGColor ? : [UIColor grayColor].CGColor;
        UIColor *tColor = self.disableTitleColor ? : [UIColor grayColor];
        [self setTitleColor:tColor forState:UIControlStateNormal];
    }
    [super setEnabled:enabled];
}

- (void)countDownButtonClick:(UIButton *)sender
{
    if (self.interceptVerifyCodeBlock) {
        BOOL intercept = self.interceptVerifyCodeBlock(self);
        // 如果需要拦截，则不执行下面的
        if (intercept) {
            return;
        }
    }
    _countDown = _countDown - 1;
    [self setTitle:[NSString stringWithFormat:@"%lds", _countDown] forState:UIControlStateNormal];
    self.enabled = NO;
    self.layer.borderColor = self.disableBorderColor.CGColor ? : [UIColor grayColor].CGColor;
    UIColor *tColor = self.disableTitleColor ? : [UIColor grayColor];
    [self setTitleColor:tColor forState:UIControlStateNormal];
    
    if (self.sendVerifyCode) {
        self.sendVerifyCode(sender);
    }
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

- (void)timerAction
{
    self.enabled = NO;
    _countDown = _countDown - 1;
    [self setTitle:[NSString stringWithFormat:@"%lds", _countDown] forState:UIControlStateNormal];
    if (_countDown == 0) {
        _countDown = self.count;
        self.enabled = YES;
        [self setTitle:@"发送验证码" forState:UIControlStateNormal];
        [self setTitleColor:self.normalTitleColor ? : [UIColor redColor] forState:UIControlStateNormal];
        self.layer.borderColor = self.normalBorderColor.CGColor ? : [UIColor redColor].CGColor;
        [self.timer invalidate];
        self.timer = nil;
        
        if (self.countDownFinished) {
            self.countDownFinished(self);
        }
    }
}

@end
